//
//  FQAddViewController.m
//  WeChat
//
//  Created by 方桥 on 16/5/17.
//  Copyright © 2016年 方桥. All rights reserved.
//

#import "FQAddViewController.h"

@interface FQAddViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;

@end

@implementation FQAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
}
- (IBAction)addBtnClick:(id)sender {
    //添加好友
    // 获取用户输入好友名称
    NSString *user = self.textField.text;
    
    //1.不能添加自己为好友
    if ([user isEqualToString:[FQAccount shareAccount].user]) {
        [self showMessage:@"不能添加自己为好友"];
        return;
    }
    //2.已经添加的好友无需添加
    XMPPJID *userJid = [XMPPJID jidWithUser:user domain:[FQAccount shareAccount].domain resource:nil];
    BOOL userExist = [[FQXmppTool sharedFQXmppTool].rosterStorage userExistsWithJID:userJid xmppStream:[FQXmppTool sharedFQXmppTool].xmppStream];
    if (userExist) {
        [self showMessage:@"你们已经是好友了"];
        return;
    }
    //3.添加好友 (订阅)
    [[FQXmppTool sharedFQXmppTool].roster subscribePresenceToUser:userJid];
}

-(void)showMessage:(NSString *)message
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
    [alertView show];
}




@end
